<!-- 预约详情 -->
<template>
  <div class="appointDetail">
    <el-card class="card">
      <div class="top_nav" v-if="pageType === 'details'">
        <div>
          <p @click="editInfo">
            <i class="iconfont icon-bianji1"></i>
            编辑
          </p>
        </div>
      </div>
      <div class="line" v-if="pageType === 'details'"></div>
      <div class="header_info">
        <div class="header_info_img">
          <img
            :src="formData.headimgurl"
            alt="加载失败"
            v-if="pageType !== 'create'"
          />
          <p v-if="pageType === 'create'">
            <i class="iconfont icon-yiyuyue1"></i>
          </p>
        </div>
        <div class="header_info_config" style="padding-left: 14px;">
          <p>预约记录</p>
          <p v-if="pageType !== 'create'">{{ formData.name }}</p>
          <p v-if="pageType === 'create'">新增预约记录</p>
        </div>
        <div class="save_info" v-if="pageType !== 'details'">
          <!-- <p @click="handleSave">
            <i class="iconfont icon-baocun"></i>
            保存
          </p> -->
          <p @click="handleSubmit('appointManage')">
            <!-- <i class="iconfont icon-close"></i> -->
            保存
          </p>
          <p @click="goToURL('appointManage')">
            <!-- <i class="iconfont icon-close"></i> -->
            关闭
          </p>
        </div>
      </div>
      <!-- 查看预约详情页面 -->
      <div class="niche_info" v-if="pageType === 'details'">
        <el-form
          ref="form"
          :model="formData"
          :rules="rules"
          label-width="120px"
        >
          <p class="niche_title">基本信息</p>
          <div class="niche_content">
            <el-form-item label="潜在顾客：" prop="user_id">
              <el-select
                v-model="formData.user_id"
                placeholder="请选择"
                @change="changeCustomer"
                disabled
              >
                <el-option
                  v-for="item in customerList"
                  :key="item.value"
                  :label="item.name"
                  :value="parseInt(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约者：" prop="username">
              <el-input
                v-model="formData.username"
                style="width: 218px;"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="性别：" prop="sex">
              <el-select v-model="formData.sex" placeholder="请选择" disabled>
                <el-option
                  v-for="item in sexList"
                  :key="item.value"
                  :label="item.label"
                  :value="parseInt(item.value)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约中心：" prop="branch_id">
              <el-select
                v-model="formData.branch_id"
                placeholder="请选择"
                disabled
              >
                <el-option
                  v-for="item in centerList"
                  :key="item.id"
                  :label="item.name"
                  :value="parseInt(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约时间：" prop="appoint_time">
              <el-date-picker
                v-model="formData.appoint_time"
                type="datetime"
                placeholder="选择时间"
                @change="changeAppointTime"
                disabled
              >
              </el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="预约编号：">
              <el-input v-model="formData.no" style="width: 218px;" :disabled="true"></el-input>
            </el-form-item> -->
            <el-form-item label="预约类型：" prop="type">
              <el-select v-model="formData.type" placeholder="请选择" disabled>
                <el-option
                  v-for="item in types"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="到访时间：">
              <el-date-picker
                v-model="formData.sex"
                type="datetime"
                placeholder="选择时间">
              </el-date-picker>
            </el-form-item> -->
            <el-form-item label="到访状态：" prop="visit_stauts">
              <el-input
                v-model="formData.visit_status === 1 ? '已到访' : '未到访'"
                style="width: 218px;"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="手机号码：">
              <el-input
                v-model="formData.mobile"
                style="width: 218px;"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="预约名称：">
              <el-input
                v-model="formData.title"
                style="width: 218px;"
                disabled
              ></el-input>
            </el-form-item>
          </div>
          <!-- <link-details :moduleData="moduleData" :formData="formData" @changeFormData="changeFormData"></link-details> -->
          <p class="niche_title">备注</p>
          <div class="niche_content remark">
            <div id="remark"></div>
          </div>
        </el-form>
      </div>
      <!-- 新增预约详情页面 -->
      <div class="niche_info" v-if="pageType === 'create'">
        <el-form
          ref="form"
          :model="formData"
          :rules="rules"
          label-width="120px"
        >
          <p class="niche_title">基本信息</p>
          <div class="niche_content">

            <el-form-item label="潜在顾客：" prop="user_id">
              <el-select
                v-model="formData.user_id"
                placeholder="请选择"
                @change="changeCustomer"
              >
                <el-option
                  v-for="item in customerList"
                  :key="item.value"
                  :label="item.name"
                  :value="parseInt(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约者：" prop="username">
              <el-input
                v-model="formData.username"
                style="width: 218px;"
                :disabled="true"
              ></el-input>
            </el-form-item>
            <el-form-item label="性别：" prop="sex">
              <el-select v-model="formData.sex" placeholder="请选择">
                <el-option
                  v-for="item in sexList"
                  :key="item.value"
                  :label="item.label"
                  :value="parseInt(item.value)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约中心：" prop="branch_id">
              <el-select v-model="formData.branch_id" placeholder="请选择">
                <el-option
                  v-for="item in centerList"
                  :key="item.id"
                  :label="item.name"
                  :value="parseInt(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约时间：" prop="appoint_time">
              <el-date-picker
                v-model="formData.appoint_time"
                type="datetime"
                placeholder="选择时间"
                @change="changeAppointTime"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="预约类型：" prop="type">
              <el-select v-model="formData.type" placeholder="请选择">
                <el-option
                  v-for="item in types"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="手机号码：">
              <el-input
                v-model="formData.mobile"
                style="width: 218px;"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="预约名称：">
              <el-input
                v-model="formData.title"
                style="width: 218px;"
                :disabled="!formData.user_id || !formData.appoint_time"
              ></el-input>
            </el-form-item>
          </div>
          <p class="niche_title">备注</p>
          <el-form-item>
            <div class="niche_content" :model="formData.remark">
              <link-editor @editor-info="getEditorInfo"></link-editor>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <!-- 编辑预约详情页面 -->
      <div class="niche_info" v-if="pageType === 'edit'">
        <el-form
          ref="form"
          :model="formData"
          :rules="rules"
          label-width="120px"
        >
          <p class="niche_title">基本信息</p>
          <div class="niche_content">
            <el-row :gutter="2">
              <el-col :span="8">
                <el-form-item label="潜在顾客：" prop="user_id">
                  <el-select
                    v-model="formData.user_id"
                    placeholder="请选择"
                    @change="changeCustomer"
                    disabled
                  >
                    <el-option
                      v-for="item in customerList"
                      :key="item.value"
                      :label="item.name"
                      :value="parseInt(item.id)"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="预约者：" prop="username">
                  <el-input
                    v-model="formData.username"
                    style="width: 218px;"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="性别：" prop="sex">
                  <el-select v-model="formData.sex" placeholder="请选择">
                    <el-option
                      v-for="item in sexList"
                      :key="item.value"
                      :label="item.label"
                      :value="parseInt(item.value)"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="2">
              <el-col :span="8">
                <el-form-item label="预约中心：" prop="branch_id">
                  <el-select v-model="formData.branch_id" placeholder="请选择">
                    <el-option
                      v-for="item in centerList"
                      :key="item.id"
                      :label="item.name"
                      :value="parseInt(item.id)"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="预约时间：" prop="appoint_time">
                  <el-date-picker
                    v-model="formData.appoint_time"
                    type="datetime"
                    placeholder="选择时间"
                    @change="changeAppointTime"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="预约类型：" prop="type">
                  <el-select v-model="formData.type" placeholder="请选择">
                    <el-option
                      v-for="item in types"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="2">
              <el-col :span="12">
                <el-form-item label="手机号码：">
                  <el-input
                    v-model="formData.mobile"
                    style="width: 218px;"
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="预约名称：">
                  <el-input
                    v-model="formData.title"
                    style="width: 218px;"
                    :disabled="!formData.user_id || !formData.appoint_time"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <!-- <link-details :moduleData="moduleData" :formData="formData" @changeFormData="changeFormData"></link-details> -->
          <p class="niche_title">备注</p>
          <el-form-item>
            <div class="niche_content" :model="formData.remark">
              <link-editor
                @editor-info="getEditorInfo"
                :editorInfo="formData.remark"
              ></link-editor>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import http from "@/api/appoint";
import customerHttp from "@/api/customer";
import globalHttp from "@/api/global";
import { detailsMinin } from "@/mixin";
import editor from "@/components/editor";
import details from "@/components/details";
import { formatDate, formatArray } from "@/utils";
export default {
  name: "appointDetail",
  mixins: [detailsMinin],
  components: {
    linkEditor: editor,
    linkDetails: details
  },
  data() {
    return {
      http: http,
      customerHttp: customerHttp,
      rules: {
        branch_id: [{ required: true, message: "请选择预约中心" }],
        user_id: [{ required: true, message: "请选择潜在顾客" }],
        type: [{ required: true, message: "请选择预约类型" }],
        // mobile: [
        //   { required: true, message: '请填写潜客手机号码' }
        // ],
        appoint_time: [{ required: true, message: "请选择预约时间" }],
        title: [{ required: true, message: "预约名称不能为空" }]
      },
      followRules: {},
      formData: {},
      followFormData: {},
      sexList: [],
      centerList: [],
      pageParams: null,
      pageType: null,
      fileList: [],
      customerList: [],
      selectName: "",
      types: [
        {
          value: "1",
          label: "线上预约"
        },
        {
          value: "2",
          label: "线下预约"
        }
      ]
    };
  },
  methods: {
    editInfo() {
      this.pageType = "edit";
      console.log(this.formData.remark);
      console.log(this.formData.remark.replace(/<[^>]+>/g, ""));
    },
    getEditorInfo(data) {
      this.formData.remark = data;
    },
    goToURL(name, params) {
      this.$router.push({ name, params });
    },
    closeNiche() {
      this.closeModal = true;
      this.noReason = false;
    },
    handlePreview() {},
    handleRemove() {},
    changeCustomer(id) {
      for (let customer of this.customerList) {
        if (customer.id === id) {
          this.formData.mobile = customer.mobile;
          this.selectName = customer.name;
        }
      }
      let Time = formatDate.toStr(
        new Date(this.formData.appoint_time),
        "YYYY-MM-DD HH:mm:ss"
      );
      if (this.formData.appoint_time) {
        this.formData.title = `${this.selectName}${Time}的预约记录`;
      }
    },
    changeAppointTime(time) {
      let Time = formatDate.toStr(new Date(time), "YYYY-MM-DD HH:mm:ss");
      if (this.formData.user_id) {
        this.formData.title = `${this.selectName}${Time}的预约记录`;
      }
    },
    // handleSave () {
    //   this.handleSubmit('appointDetail')
    //   this.goToURL('appointDetail', {pageType: 'details'})
    // },
    init() {
      let userData = JSON.parse(sessionStorage.getItem("user"));
      let basicConfig = JSON.parse(sessionStorage.getItem("basicConfig"));
      this.sexList = formatArray(basicConfig.sex);
      this.formData.center_id = userData.center_id;
      this.formData.username = userData.username;
      this.getPageParams("appointDetailParams", () => {
        this.getCommonModuleFields("student");
      });
    }
  },
  created() {
    this.init();
    customerHttp.getList().then(res => {
      this.customerList = res.data.data;
      let center_id = res.query.center_id;
      globalHttp.getCenterList({ center_id }).then(res => {
        this.centerList = res.data;
      });
    });
  },
  mounted() {
    if (this.pageParams.id) {
      let id = this.pageParams.id;
      this.http.getContentDetail({ id }).then(res => {
        this.formData = res.data;
        showRemark(this.formData.remark);
      });
    }
    this.formData.remark = this.editorContent;
  }
};
function showRemark(html) {
  var remark = document.getElementById("remark");
  remark.innerHTML = html;
  var imgs = remark.getElementsByTagName("img");
  for (let img of imgs) {
    img.style.width = "100px";
  }
}
</script>

<style lang="scss" scoped>
.appointDetail {
  padding: 10px;
  .card {
    .header_info {
      position: relative;
      padding: 26px 0 45px 34px;
      display: flex;
      &_img {
        width: 47px;
        height: 47px;
        overflow: hidden;
        border-radius: 50%;
        img {
          width: 47px;
          height: 47px;
        }
        p {
          width: 47px;
          height: 47px;
          display: flex;
          justify-content: center;
          align-items: center;
          background: linear-gradient(to top left, #f25751, #f27450);
          border-radius: 10px;
          i {
            color: white;
            font-size: 24px;
          }
        }
      }
      &_config {
        padding-left: 26px;
        padding-right: 26px;
        // border-right: 1px solid #bfbfbf;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        &:last-of-type {
          border-right: none;
        }
        p {
          &:nth-of-type(1) {
            font-size: 14px;
            color: #565656;
          }
          &:nth-of-type(2) {
            font-size: 20px;
            color: #333;
            font-weight: 600;
          }
        }
      }
      .save_info {
        display: flex;
        position: absolute;
        right: 40px;
        top: 36px;
        p {
          margin-left: 20px;
          border-radius: 5px;
          cursor: pointer;
          &:nth-of-type(1) {
            color: #1799eb;
            padding: 9px 22px;
            background: #e5f4ff;
          }
          &:nth-of-type(2) {
            color: #1799eb;
            padding: 9px 22px;
            background: #e5f4ff;
          }
          /* &:nth-of-type(2) {
            color: white;
            padding: 9px 12px;
            background: linear-gradient(to right, #17b5eb, #1796eb);
          } */
        }
      }
    }
    .niche_info {
      .niche_title {
        width: 100%;
        line-height: 41px;
        background: #f5f5f5;
        padding-left: 24px;
        color: #333;
        font-size: 20px;
        font-weight: 600;
      }
      .niche_content {
        padding: 40px 0 44px 24px;
        padding-top: 40px;
        display: flex;
        flex-wrap: wrap;
        /deep/ .el-form-item {
        //   width: 25%;
          font-weight: 600;
        }
      }
      #remark {
        img {
          display: block;
          width: 100px !important;
        }
      }
    }
    .top_nav {
      padding-left: 34px;
      div {
        display: flex;
        padding: 25px 0;
        p {
          margin-right: 20px;
          color: #565656;
          font-size: 16px;
          cursor: pointer;
          &:hover {
            color: #1796eb;
          }
        }
      }
    }
    .line {
      height: 1px;
      width: 100%;
      background: #bfbfbf;
    }
  }
  .contract_pic {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    img {
      width: 191px;
      height: 171px;
    }
  }
  .btn {
    margin-top: 30px;
    width: 100%;
    display: flex;
    justify-content: center;
    /deep/ .el-button {
      width: 214px;
    }
  }
  .follow_customer {
    display: flex;
    width: 100%;
    .follow_form {
      width: 100%;
      /deep/ .el-upload-list {
        display: flex;
        li {
          width: 20%;
          margin-right: 10px;
          .el-upload-list__item-name {
            display: none;
          }
        }
      }
      .el-textarea {
        width: 491px;
      }
      .el-input {
        width: 179px;
      }
      .el-select {
        width: 179px;
      }
    }
  }
}
</style>
